import os
import threading

import pystray
import sqlalchemy
from flask import Flask
from PIL import Image
from pystray import Icon, MenuItem

from src.api import api_bp, thread_pool
from src.config import config
from src.db import db
from src.task import scheduler
from src.util import print_static_vars

db.connect()
scheduler.start()


app = Flask(__name__)
app.register_blueprint(api_bp)

# @app.teardown_appcontext
# def on_closed(exception=None):
#     print('on_closed')
#     scheduler.shutdown()
#     thread_pool.shutdown()
#     db.close()

if __name__ == '__main__':  
    def show():
        pass  
    stray_memu = (MenuItem('打开', show, default=True), MenuItem('退出', quit))
    image = Image.open("logo.png")
    icon = pystray.Icon("name", image, "鼠标移动到\n托盘图标上\n展示内容", stray_memu)
    threading.Thread(target=icon.run, daemon=True).start()

    print_static_vars(config)
    app.run()
